package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.*;
import com.qf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/role")
public class DtsAdminRoleController {

    @Autowired
    DtsRoleService roleService;
    @Autowired
    ApplicationContext context;
    @Autowired
    DtsPermissionService permissionService;

    @PostMapping("/permissions")
    public Object grantPermissions(@RequestBody Map<String,Object> map){

        Integer roleId = (Integer) map.get("roleId");
        if(roleService.isSuperAdmin(roleId)){
            return ResponseUtil.fail(-1,"超级管理员不能修改权限");
        }

        ArrayList<String> permissions = (ArrayList<String>) map.get("permissions");

        permissionService.updatePermission(roleId,permissions);
        return ResponseUtil.ok();
    }

    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        //获取控制器上所有方法的权限列表
        List<Permission> permissions = PermissionUtil.listPermission(context, "com.qf.controller");
        //获取所有权限的字符串列表
        Set<String> assignedPermissions = PermissionUtil.listPermissionString(permissions);
        //把所有权限信息转换为前端所需要的格式
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);

        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions",systemPermissions);

        //如果该角色为超级管理员拥有所有权限不是只拥有自己所拥有的权限

        data.put("assignedPermissions",assignedPermissions);
        if(roleService.isSuperAdmin(roleId)){
            data.put("assignedPermissions",assignedPermissions);
        }else {
            Integer[] integers = new Integer[]{roleId};
            Set<String> permByIds = permissionService.findPermByIds(integers);
            data.put("assignedPermissions",permByIds);
        }
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole dtsRole){
        boolean flag = roleService.delete(dtsRole);
        if(flag){
            return ResponseUtil.ok();
        }else {
            return ResponseUtil.fail(-1,"该用户还在使用无法删除");
        }
    }

    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole dtsRole){
        roleService.update(dtsRole);

        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole dtsRole){
        DtsRole dtsRole1 = roleService.insert(dtsRole);
        return ResponseUtil.ok(dtsRole1);
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
            String sort, String desc){

        IPage<DtsRole> ipage = roleService.queryPage(page,limit,sort,desc);
        Map<String,Object> data = new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/options")
    public Object options(){

        List<DtsRole> roles = roleService.findAll();
        List<Map<String, Object>> data = roles.stream().map(dtsRole -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsRole.getId());
            map.put("label", dtsRole.getName());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);
    }
}
